require(["common", "jQuery", "coAjax", "queryString", "hash_routie"], function(jSouper, $, coAjax, QueryString, hash_routie) {
	hash_routie.on("points", function() {
		App.set('title', '推荐返利');
		coAjax.get(appConfig.bus.user_bind, function(result) {
			console.log(result.result);
			App.set("$Cache.bus.bus_user_bind", result.result);
			_computer_money();
		});
	});
	coAjax.get(appConfig.bus.recommend_config, function(result) {
		App.set("$Cache.bus.recommend_config", result.result);
		_computer_money();
	});
	coAjax.get(appConfig.bus.recommend_discount, function(result) {
		App.set("$Cache.bus.recommend_discount", result.result);
	});
	//计算金额
	function _computer_money() {
		var bus_user_bind = App.get("$Cache.bus.bus_user_bind");
		var recommend_config = App.get("$Cache.bus.recommend_config");
		if (bus_user_bind && recommend_config) {
			bus_user_bind.forEach(function(info) {
				for (var i = recommend_config.length - 1, config; config = recommend_config[i]; i -= 1) {
					var max_price = parseFloat(config.max_price) || 0;
					if (max_price <= info.acc) {
						var rate = parseFloat(config.rate) || 0;
						info._money = info.acc * rate;
						break;
					}
				}
			});
			App.set("$Cache.bus.bus_user_bind", bus_user_bind);
		}
	};

	function _upload_recommend_config() {
		var recommend_config = App.get("$Cache.bus.recommend_config");
		recommend_config = recommend_config.sort(function(a, b) {
			return a.max_price > b.max_price
		});
		coAjax.put(appConfig.bus.update_recommend_config, {
			recommend_config: recommend_config
		}, function(result) {
			alert("success", "推荐返利规则修改成功")
			App.set("$Cache.bus.recommend_config", result.result);
			_computer_money();
		});
	};
	App.set("$Top.$Event.recommend_config.add", function() {
		var recommend_config = App.get("$Cache.bus.recommend_config");
		var new_recommend_config = App.get("$Cache.new_recommend_config");
		recommend_config.push({
			max_price: +new_recommend_config.max_price,
			rate: +new_recommend_config.rate
		});
		//清空
		App.set("$Cache.new_recommend_config", {});
		_upload_recommend_config();
		alert('success', '添加成功！');
	});
	App.set("$Top.$Event.recommend_config.update", function(e, vm) {
		var is_updatting = vm.get("$Private.is_updatting");
		if (is_updatting) {
			vm.set("$Private.is_updatting", false);
			_upload_recommend_config();
		} else {
			vm.set("$Private.is_updatting", true);
		}
		console.log(is_updatting, vm.get("$Private.is_updatting"));
	});
	App.set("$Top.$Event.recommend_config.remove", function(e, vm) {
		vm.removeFromArray();
		_upload_recommend_config();
	});
	App.set("$Event.bus.shenqingduihuanzhong_to_shangjiayijingfukuan", function(e, vm) {
		coAjax.put(appConfig.bus.res_shenqingduihuanzhong_to_shangjiayijingfukuan, {
			user_id: vm.get("user_id")
		}, function(result) {
			vm.set(result.result);
		});
	});
	//获取推荐折扣

	App.set("$Event.bus.update_recommed_discount", function() {
		coAjax.put(appConfig.bus.update_recommend_discount, {
			discount_value: App.get("$Cache.bus.recommend_discount")
		}, function(result) {
			alert("success", "推荐折扣修改成功")
			App.set("$Cache.bus.recommend_discount", result.result);
		});
	});
});